
export function getDistinctArray (arr) {
  return Array.from(new Set(arr))
}

export function getType (val) {
  return Object.prototype.toString.call(val).slice(8, -1).toLowerCase()
}

export function isPromise (obj) {
  return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
}

function parseVersion (version) {
  if (!version) {
    return
  }
  const matches = version.match(/^(\d+).(\d+).(\d+)/)
  if (!matches) {
    return
  }
  return matches.slice(1, 4).map(item => parseInt(item))
}

function compareArray (a, b) {
  const minLength = Math.max(a.length, b.length)
  const result = 0
  for (let i = 0; i < minLength; i++) {
    const valA = a[i]
    const valB = b[i]
    if (valA > valB) {
      return 1
    } else if (valA < valB) {
      return -1
    }
  }
  return result
}

export function compareUniIdVersion (a, b) {
  const arrA = parseVersion(a)
  const arrB = parseVersion(b)
  if (!arrA) {
    if (!arrB) {
      return 0
    }
    return -1
  }
  if (!arrB) {
    return 1
  }
  return compareArray(arrA, arrB)
}
